/**
 * 武器item
 */
class WeaponSmallItem extends BaseComp{
    public img_select:eui.Image;
    private img_icon:eui.Image;
    private img_mask:eui.Image;
    private img_experience:eui.Image;
    
    constructor()
    {
        super('weaponSmallItemView');
    }

    public init()
    {
        
    }

    public show(data)
    {
        this.img_select.visible = false;
        if(data)
        {
            this.data = data;
            this.img_icon.source = data.itemData.icon;
            let logic:WeaponNewSelectUI = UIManager.instance().getLogicByName(UIConst.NEW_WEAPONSELECT_UI) as WeaponNewSelectUI;
            
            if((data.index == 1 && !logic.curSelectItem) || (logic.curSelectItemId == this.data.itemData.id))
            {
                this.setSelectState();
            }
            let weaponId = this.data.weaponData.id;
            let isHave = RoleCache.instance().judgeIsHaveWeapon(weaponId) || RoleCache.instance().isCurExperience(weaponId);
            if(isHave)
            {
                this.img_mask.visible = false;
            }
            else
            {
                this.img_mask.visible = true;
            }
            if(RoleCache.instance().isCurExperience(weaponId) && !RoleCache.instance().judgeIsHaveWeapon(weaponId))
            {
                this.img_experience.visible = true;
            }
            else
            {
                this.img_experience.visible = false;
            }
        }
    }

    public update()
    {
        let isHave = RoleCache.instance().judgeIsHaveWeapon(this.data.weaponData.id);
        if(isHave)
        {
            this.img_mask.visible = false;
        }
        else
        {
            this.img_mask.visible = true;
        }
    }

    public addEvent()
    {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.touchHandle, this);
    }

    protected touchHandle()
    {
        let logic:WeaponNewSelectUI = UIManager.instance().getLogicByName(UIConst.NEW_WEAPONSELECT_UI) as WeaponNewSelectUI;
        if(logic)
        {
            if(logic.curSelectItemId == this.getData().itemData.id)return;
            this.setSelectState();
        }
    }

    public setSelectState()
    {
        this.img_select.visible = true;
        let logic:WeaponNewSelectUI = UIManager.instance().getLogicByName(UIConst.NEW_WEAPONSELECT_UI) as WeaponNewSelectUI;
        if(logic)
        {
            logic.setSelcet(this);
        }
    }

    public getData()
    {
        return this.data;
    }

    public cancelSelectState()
    {
        this.img_select.visible = false;
    }

    public removeEvent()
    {
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.touchHandle, this);
    }

    public clear()
    {
        super.clear();
    }
}

window['WeaponSmallItem'] = WeaponSmallItem;